package com.secxiot.provider.user.service.impl;

import com.secxiot.common.constant.ServerStatusEnum;
import com.secxiot.common.exception.BaseRuntimeException;
import com.secxiot.common.utils.IdWorker;
import com.secxiot.provider.user.dao.IntegralDao;
import com.secxiot.provider.user.dao.UserMapper;
import com.secxiot.provider.user.entity.SysExchangeRecord;
import com.secxiot.provider.user.entity.SysExchangeRules;
import com.secxiot.provider.user.entity.UserAccount;
import com.secxiot.provider.user.entity.UserDo;
import com.secxiot.provider.user.service.IntegralService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Log4j2
@Transactional
@Service
public class IntegralServiceImpl implements IntegralService {

    @Autowired
    private IntegralDao integralDao;

    @Autowired
    private UserMapper userMapper;

    /**
     * 获取兑换规则
     *
     * @param userId
     * @return
     */
    @Override
    public List<SysExchangeRules> getExchangeRules(Long userId) {
        UserDo userDo = userMapper.getById(userId);
        return integralDao.getExchangeRules(userDo.getHospitalId());
    }

    /**
     * 积分兑换
     *
     * @param userId          用户ID
     * @param integralNum     积分数量
     * @param exchangeRulesId 规则ID
     * @return
     */
    @Override
    public Integer integralExchange(Long userId, Long integralNum, Long exchangeRulesId) {
        IdWorker idWorker = new IdWorker();
        UserDo userDo = userMapper.getById(userId);
        SysExchangeRules exchangeRules = integralDao.getExchangeRulesById(userDo.getHospitalId(), exchangeRulesId);


        SysExchangeRecord exchangeRecord = new SysExchangeRecord();
        exchangeRecord.setUserId(userDo.getUserId());
        exchangeRecord.setExchangeRecordId(idWorker.nextId());
        exchangeRecord.setExchangeRulesId(exchangeRulesId);

        UserAccount userAccount = userMapper.getUserAccount(userId);
        if (null == userAccount) {
            throw new BaseRuntimeException(ServerStatusEnum.ACCOUNT_NULL);
        }
        exchangeRecord.setAccountId(userAccount.getAccountId());
        exchangeRecord.setGoodsCode(exchangeRules.getGoodsCode());
        exchangeRecord.setGoodsName(exchangeRules.getGoodsName());
        exchangeRecord.setGoodsUrl(exchangeRules.getGoodsUrl());
        exchangeRecord.setGoodsPrice(exchangeRules.getGoodsPrice());
        exchangeRecord.setGoodsIntegralNum(exchangeRules.getIntegralNum());
        exchangeRecord.setIntegralNum(integralNum);
        exchangeRecord.setHospitalId(userDo.getHospitalId());
        exchangeRecord.setState(1);
        exchangeRecord.setStatus(1);
        exchangeRecord.setCreaterId(userDo.getUserId());
        exchangeRecord.setCreateTime(new Date());
        Integer aLong = integralDao.saveIntegralExchange(exchangeRecord);

        userDo.setIntegralCount(userDo.getIntegralCount() - integralNum);
        userMapper.update(userDo);
        return aLong;
    }

    /**
     * 获取兑换列表
     *
     * @param userId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public List<SysExchangeRecord> getExchangeRecordList(Long userId, Long pageNo, Long pageSize) {
        return integralDao.getExchangeRecordList(userId, pageNo, pageSize);
    }
}
